import { login, logout, getUserInfo, socialLogin, smsLogin } from '@/api/login';
import { listSimpleUsers } from '@/api/system/user';
import { setToken, removeToken } from '@/utils/auth';
import { isString } from 'lodash';

const user = {
  state: {
    id: 0, // 用户编号
    name: '',
    avatar: '',
    roles: [],
    permissions: [],
    allUsers: null,
    currentRoleId:''
  },

  mutations: {
    SET_ID: (state, id) => {
      state.id = id
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_NICKNAME: (state, nickname) => {
      state.nickname = nickname
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions
    },
    SET_ALLUSERS: (state, allUsers) => {
      state.allUsers = allUsers
    },
    SET_CURRENT_ROLE_ID: (state, currentRoleId) => {
      state.currentRoleId = currentRoleId
    }
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim()
      const password = userInfo.password
      const captchaVerification = userInfo.captchaVerification
      const socialCode = userInfo.socialCode
      const socialState = userInfo.socialState
      const socialType = userInfo.socialType
      return new Promise((resolve, reject) => {
        login(username, password, captchaVerification, socialType, socialCode, socialState).then(res => {
          res = res.data;
          // 设置 token
          setToken(res)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 社交登录
    SocialLogin({ commit }, userInfo) {
      const code = userInfo.code
      const state = userInfo.state
      const type = userInfo.type
      return new Promise((resolve, reject) => {
        socialLogin(type, code, state).then(res => {
          res = res.data;
          // 设置 token
          setToken(res)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 短信登录
    SmsLogin({ commit }, userInfo) {
      const mobile = userInfo.mobile.trim()
      const mobileCode = userInfo.mobileCode
      return new Promise((resolve, reject) => {
        smsLogin(mobile, mobileCode).then(res => {
          res = res.data;
          // 设置 token
          setToken(res)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    async getUserInfo({ commit }) {
      const { data } = await getUserInfo();

      const user = data?.user || {};
      const avatar = isString(user?.avatar) ? user.avatar : require("@/assets/images/profile.jpg");

      commit('SET_ID', user.id);
      commit('SET_NAME', user.userName);
      commit('SET_NICKNAME', user.nickname);
      commit('SET_CURRENT_ROLE_ID', user.currentRoleId);
      commit('SET_AVATAR', avatar);
      commit('SET_ROLES', data?.roles || []);
      commit('SET_PERMISSIONS', data?.permissions || []);
    },

    // 退出系统
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_ROLES', [])
          commit('SET_PERMISSIONS', [])
          commit('SET_CURRENT_ROLE_ID', '');
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    async getAllUser({ commit, state }) {
      if (!state.allUsers) {
        // TODO: 这里要开启队列   后面过来的 等待上一个请求完成后直接返回  不在发起请求了
        const res = await listSimpleUsers();
        commit('SET_ALLUSERS', res.data);
      }
      return state.allUsers || [];
    }
  }
}

export default user
